package com.lepu.app.widget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.AppEnterTongJi;
import com.app.utils.BitmapLoadUtil;
import com.app.utils.MyNetUtil;
import com.app.utils.Setting;
import com.app.utils.Utils;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.ui.auto_viewpager.AutoScrollViewPager;
import com.core.lib.ui.auto_viewpager.AutoScrollViewPagerAdapter;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.eyzhs.app.R;
import com.lepu.app.fun.index.activity.IndexWriterBannerActivity;
import com.lepu.app.fun.knowledge.bean.Article;
import com.lepu.app.fun.lottery.AppLotteryActivity;
import com.lepu.app.fun.my.PubContentDetailActivity;
import com.lepu.app.main.activity.AppBroswerActivity;

/**
 * 
 * @author wxd
 * 
 */
public class FocusView extends LinearLayout {

	private Context mContext;
	private ArrayList<Article> mFocusList;

	public AutoScrollViewPager mAutoScrollViewPager;
	private AutoScrollViewPagerAdapter mPagerAdapter;
	private LinearLayout mFocusIndicator;
	private TextView mFocusName;

	private int mFocusPageCount;
	private BaseFragmentActivity mBaseFragmentActivity = null;
	private int mHeight = 0;
	private FocusViewClickListener mFocusViewClickListener = null;
	private int mDefaultEmptyBannerResId = 0;
	private int mDefaultLoadBannerResId = 0;
	SwipeRefreshLayout mGoogleRefreshLayout;

	public FocusView(Context context, ArrayList<Article> dataList, int height,
			FocusViewClickListener focusViewClickListener,
			int defaultEmptyBannerResId, int defaultLoadBannerResId,SwipeRefreshLayout googleRefreshLayout) {
		super(context);

		mContext = context;
		mFocusList = dataList;
		mHeight = height;
		mFocusViewClickListener = focusViewClickListener;
		mDefaultEmptyBannerResId = defaultEmptyBannerResId;
		mDefaultLoadBannerResId = defaultLoadBannerResId;

		mBaseFragmentActivity = (BaseFragmentActivity) AppManager
				.getAppManager().currentActivity();
		mGoogleRefreshLayout = googleRefreshLayout;

		LayoutInflater.from(mContext).inflate(R.layout.main_focus_view_acticle,
				this, true);
		init();
	}

	public FocusView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@SuppressWarnings("deprecation")
	private void init() {

		FrameLayout contentLayout = (FrameLayout) findViewById(R.id.contentLayout);

		WindowManager wm = (WindowManager) mContext
				.getSystemService(Context.WINDOW_SERVICE);

		int width = wm.getDefaultDisplay().getWidth();
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		params.width = width;
		params.height = mHeight;// width * 9 / 16;
		

		if (mFocusList == null || mFocusList.size() == 0) {

			if (mDefaultEmptyBannerResId > 0) {
				contentLayout.setLayoutParams(params);
				
				ImageView imageView = new ImageView(mBaseFragmentActivity);
				imageView.setLayoutParams(new ViewGroup.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT,
						ViewGroup.LayoutParams.MATCH_PARENT));
				imageView.setScaleType(ImageView.ScaleType.FIT_XY);
				imageView.setBackgroundResource(mDefaultEmptyBannerResId);

				imageView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mFocusViewClickListener != null) {
							mFocusViewClickListener.noFocusClick();
						}
					}
				});
				contentLayout.addView(imageView);
			} else {
				// 不需要默认 banner图
			}

		} else if (mFocusList.size() == 1) {
			contentLayout.setLayoutParams(params);
			
			ImageView imageView = beanFocusClick(mFocusList.get(0));
			contentLayout.addView(imageView);

		} else {
			contentLayout.setLayoutParams(params);
			
			mAutoScrollViewPager = (AutoScrollViewPager) findViewById(R.id.autoScrollViewPager);
			mAutoScrollViewPager.setVisibility(View.VISIBLE);

			mFocusIndicator = (LinearLayout) findViewById(R.id.focusIndicator);
			mFocusName = (TextView) findViewById(R.id.focusName);

			int count = 0;
			if (mFocusList != null && mFocusList.size() > 0) {
				count = mFocusList.size();
			}

			for (int i = 0; i < count; i++) {
				View v = new View(mContext);
				v.setBackgroundResource(R.drawable.app_point_bg);
				LayoutParams layoutParams = new LayoutParams(15, 15);
				layoutParams.setMargins(5, 5, 5, 5);
				mFocusIndicator.addView(v, layoutParams);
			}

			mPagerAdapter = new AutoScrollViewPagerAdapter(mContext) {

				@Override
				public List<View> buildViews() {
					return addView();
				}
			};

			mAutoScrollViewPager.setAdapter(mPagerAdapter);
			mFocusPageCount = mPagerAdapter.getRealPageCount();

			mAutoScrollViewPager.setInterval(2000);
			mAutoScrollViewPager
					.setOnPageChangeListener(new MyOnPageChangeListener());
			mAutoScrollViewPager.setAutoScrollDurationFactor(10);
			mAutoScrollViewPager.start();

			if(mGoogleRefreshLayout != null)
			{
				mAutoScrollViewPager.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						switch (event.getAction()) {
							case MotionEvent.ACTION_MOVE:
								mGoogleRefreshLayout.setEnabled(false);
								break;
							case MotionEvent.ACTION_UP:
							case MotionEvent.ACTION_CANCEL:
								mGoogleRefreshLayout.setEnabled(true);
								break;
						}
						return false;
					}
				});
			}
		}
	}

	public void onResumeViewPager() {
		if (mAutoScrollViewPager != null) {
			mAutoScrollViewPager.onResume();
		}
	}

	public void onPauseViewPager() {
		if (mAutoScrollViewPager != null) {
			mAutoScrollViewPager.onPause();
		}
	}

	private ImageView beanFocusClick(final Article item) {
		ImageView imageView = new ImageView(mContext);
		imageView.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);

		String imageUrl = Setting.getImageUrl(item.Cover);
//		UniversalImageLoadTool.disPlayOrign(imageUrl, imageView,
//				mDefaultLoadBannerResId);
		BitmapLoadUtil.displayImage(mDefaultLoadBannerResId, imageUrl, imageView);

		imageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {

				Utils.clickFocusItem(mBaseFragmentActivity,item);
			}
		});
		return imageView;
	}

	private List<View> addView() {

		List<View> list = new ArrayList<View>();

		if (mFocusList != null && mFocusList.size() > 0) {
			for (int i = 0; i < mFocusList.size(); i++) {
				final Article item = mFocusList.get(i);
				list.add(beanFocusClick(item));
			}
		}

		return list;
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			int pos = position % mFocusPageCount;
			for (int i = 0; i < mFocusIndicator.getChildCount(); i++) {
				mFocusIndicator.getChildAt(i).setEnabled(false);
			}

			mFocusIndicator.getChildAt(pos).setEnabled(true);
			mFocusName.setVisibility(View.GONE);
			mFocusName.setText(mFocusList.get(pos).ACTitle);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	public interface FocusViewClickListener {
		void noFocusClick();
	}

}
